/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package QLNV.Bean;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import QLNV.Utilities.*;
import QLNV.Entity.*;
import java.sql.*;
import java.util.*;
/**
 *
 * @author Ice
 */
public class PhongBanForm extends org.apache.struts.action.ActionForm {
    
    private String name;
    private int number;
    private String mapban;
    private String tenpban;
    private String message;

    /**
     * @return
     */
    public String getName() {
        return name;
    }

    /**
     * @param string
     */
    public void setName(String string) {
        name = string;
    }

    /**
     * @return
     */
    public int getNumber() {
        return number;
    }

    /**
     * @param i
     */
    public void setNumber(int i) {
        number = i;
    }

    /**
     *
     */
    public PhongBanForm() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param request The HTTP Request we are processing.
     * @return
     */
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if (getName() == null || getName().length() < 1) {
            errors.add("name", new ActionMessage("error.name.required"));
            // TODO: add 'error.name.required' key to your resources
        }
        return errors;
    }

    /**
     * 
     * @return
     * @throws SQLException 
     */
    public ArrayList<PhongBan> view() throws SQLException {
        ArrayList<PhongBan> pBanList = new ArrayList<PhongBan>();
        try {
//            DBHelper.getInstance().initConnection();
//            ResultSet rs = DBHelper.getInstance().executeQueryProc("View_Project", null);
            Conn cnn = new Conn();
            String sql = "select * from phongban";
            ResultSet rs = cnn.exec(sql);
            if(rs!=null){
                while (rs.next()) {
                    PhongBan pban = new PhongBan();
                    pban.setMaPban(rs.getString("MAPBAN"));
                    pban.setTenPban(rs.getString("TENPBAN"));
                    pBanList.add(pban);
                }
            }
            cnn.close();
        } catch (SQLException e) {
            e.getMessage();
        }
        return pBanList;
    }
    
//    public int insert (String username, String password) throws SQLException{
//        int ins = 0;
//        try{
//            Conn cnn = new Conn();
//            String ins_sql = "insert into phongban(mapban, tenpban) value (?,?)";
//            PreparedStatement prst = cnn.getPrepareStatement(ins_sql);
//            prst.setString(1, username);
//            prst.setString(2, password);
//            ins = prst.executeUpdate();
//        }
//        catch (Exception e){
//            e.getMessage();
//        }
//        return ins;
//    }

    /**
     * insert new phongban by calling Insert_PhongBan procedure
     * @param maPban
     * @param tenPban
     * @return
     * @throws SQLException 
     */
    public String insert (String mapban, String tenpban) throws SQLException{
        
        DBHelper.getInstance().initConnection();
        ArrayList<String> arr = new ArrayList<String>();
        arr.add(mapban);
        arr.add(tenpban);
        String message = DBHelper.getInstance().executeUpdateProcReturn("Insert_PhongBan", arr);
        DBHelper.getInstance().closeConnection();
        return message;
    }
    
    /**
     * delete
     * @param maPban
     * @return
     * @throws SQLException 
     */
    public String delete (String maPban) throws SQLException{

        String message = "";
        DBHelper.getInstance().initConnection();
        ArrayList<String> arr = new ArrayList<String>();
        arr.add(maPban);
        message = DBHelper.getInstance().executeUpdateProcReturn("Delete_PhongBan", arr);
        return message;
    }
    
    public int update (String mapban, String tenpban) throws SQLException{
        int row = 0;
        Conn cnn = new  Conn();
        String sql_ups = "update phongban set tenpban=? where mapban=?";
        PreparedStatement prstm = cnn.getPrepareStatement(sql_ups);
        prstm.setString(1, tenpban);
        
        prstm.setString(2, mapban);
        row = prstm.executeUpdate();
        return row;
    }
    
    public ArrayList<PhongBan> search (String mapban) throws SQLException{
        PhongBan pBan = new PhongBan();
        ArrayList<PhongBan> pBanList = new ArrayList<PhongBan>();
        DBHelper.getInstance().initConnection();
        ArrayList<String> arr = new ArrayList<String>();
        arr.add(mapban);
        ResultSet rs = DBHelper.getInstance().executeQueryProc("Search_PhongBan", arr);  
        if(rs!=null){
             while (rs.next()) {
                ResultSetMetaData rsmd = rs.getMetaData();
                int numberOfColumns = rsmd.getColumnCount();
                if (numberOfColumns == 1) {
                    pBan.setMessage(rs.getString("message"));
                } else {
                    pBan.setMaPban(rs.getString("mapban"));
                    pBan.setTenPban(rs.getString("tenpban"));
                    pBanList.add(pBan);
                }
            }
        }
        return pBanList;
        //String message = DBHelper.getInstance().executeUpdateProcReturn("Search_PhongBan", arr);
        
    }
    /**
     * @return the message
     */
    public String getMessage() {
        return message;
    }

    /**
     * @param message the message to set
     */
    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * @return the mapban
     */
    public String getMapban() {
        return mapban;
    }

    /**
     * @param mapban the mapban to set
     */
    public void setMapban(String mapban) {
        this.mapban = mapban;
    }

    /**
     * @return the tenpban
     */
    public String getTenpban() {
        return tenpban;
    }

    /**
     * @param tenpban the tenpban to set
     */
    public void setTenpban(String tenpban) {
        this.tenpban = tenpban;
    }
}
